#include "purchasereceipt.h"
#include "ui_purchasereceipt.h"
#include "calendar.h"
#include <QDateTime>
#include <QKeyEvent>
#include <QDebug>
#include <QDesktopWidget>
#include <QMessageBox>
#include "purchaseorderform.h"
#include "inquiresql.h"

PurchaseReceipt::PurchaseReceipt(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PurchaseReceipt)
{
    ui->setupUi(this);
    this->setLayout(ui->MainLayout);
    this->setFixedSize(762,400);
    setWindowTitle(tr("进货订单"));

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    setWindowFlags(Qt::WindowStaysOnTopHint | Qt::Tool );

    QHeaderView* vheaderView = ui->tableWidgetPurchaseOrderForm->verticalHeader();
    vheaderView->setHidden(true);

    QStringList headers;
    headers << tr("进货日期") << tr("进货单号") << tr("供货商") << tr("操作人员") << tr("备注信息");

    ui->tableWidgetPurchaseOrderForm->setColumnCount(5);
    ui->tableWidgetPurchaseOrderForm->setHorizontalHeaderLabels(headers);

    ui->tableWidgetPurchaseOrderForm->setColumnWidth(0,110);
    ui->tableWidgetPurchaseOrderForm->setColumnWidth(1,150);
    ui->tableWidgetPurchaseOrderForm->setColumnWidth(2,200);
    ui->tableWidgetPurchaseOrderForm->setColumnWidth(3,100);
    ui->tableWidgetPurchaseOrderForm->setColumnWidth(4,200);

    setQtableWidgetHeaderStyle();

    ui->lineEditDateStart->installEventFilter(this);
    ui->lineEditDateEnd->installEventFilter(this);

    m_calendar = new Calendar(this);
    m_calendar->hide();

    connect(m_calendar, SIGNAL(signalUpdateLineEditDate(QString)), this, SLOT(slotUpdateLineEditDate(QString)));

//    QDateTime time = QDateTime::currentDateTime();//获取系统现在的时间
//    QString str = time.toString("yyyy-MM-dd"); //设置显示格式
//    ui->lineEditDateStart->setText(str);
//    ui->lineEditDateEnd->setText(str);

    LineEdit = -1;
    LineEditEnable = true;

    //初始化显示当天的进货订单
    initLineEditTime();
//    ui->lineEditDateStart->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd"));
//    ui->lineEditDateEnd->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd"));
//    displayTableWidgetPurchaseOrderForm();
}

PurchaseReceipt::~PurchaseReceipt()
{
    delete ui;
}

bool PurchaseReceipt::eventFilter(QObject *target, QEvent *event)
{
    if ( target == ui->lineEditDateEnd || target == ui->lineEditDateStart )
    {
        //鼠标左键释放事件
        if (event->type() == QEvent::MouseButtonRelease)
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
            if (mouseEvent->button() == Qt::LeftButton)
            {
                QString time;
                if (target == ui->lineEditDateEnd)
                {
                    LineEdit = end;
                    time = ui->lineEditDateEnd->text();
                }
                else if (target == ui->lineEditDateStart)
                {
                    LineEdit = start;
                    time = ui->lineEditDateStart->text();
                }
                if (LineEditEnable)
                {
                    m_calendar->move((QApplication::desktop()->width() - m_calendar->width())/2,
                                     (QApplication::desktop()->height() - m_calendar->height())/2);
                    m_calendar->setSelectedCurrentDate(time);
                    m_calendar->setWindowFlags(
                                Qt::FramelessWindowHint | Qt::Dialog);

                    if (LineEdit == start)
                    {
                        QPoint GlobalPoint(ui->lineEditDateStart->mapToGlobal(QPoint(0, 0)));
                        int x = GlobalPoint.x();
                        int y = GlobalPoint.y();
                        m_calendar->setGeometry(x , y + 35, 150, 130);
                    }
                    else if (LineEdit == end)
                    {
                        QPoint GlobalPoint(ui->lineEditDateEnd->mapToGlobal(QPoint(0, 0)));
                        int x = GlobalPoint.x();
                        int y = GlobalPoint.y();
                        m_calendar->setGeometry(x , y + 35, 150, 130);
                    }
                    //在对 QWidget 操作时，不能操作其他东西
//                    m_calendar->setWindowModality(Qt::ApplicationModal);
//                    m_calendar->setAttribute(Qt::WA_ShowModal, true);
                    m_calendar->show();
                }
            }
        }
    }
    return QWidget::eventFilter(target, event);
}

void PurchaseReceipt::slotUpdateLineEditDate(QString date)
{
    if (LineEdit == start)
    {
        ui->lineEditDateStart->setText(date);        
        return;
    }
    if (LineEdit == end)
    {
        ui->lineEditDateEnd->setText(date);
        return;
    }
    LineEdit = -1;
}

void PurchaseReceipt::setQtableWidgetHeaderStyle()
{
    for (int i = 0; i < 5; ++i)
    {
        ui->tableWidgetPurchaseOrderForm->horizontalHeaderItem(i)->setFont(QFont("Helvetica"));
        ui->tableWidgetPurchaseOrderForm->horizontalHeaderItem(i)->setTextColor(QColor(200,111,30));
    }
}

void PurchaseReceipt::displayTableWidgetPurchaseOrderForm()
{
    m_list.clear();
    ui->tableWidgetPurchaseOrderForm->clear();

    if (LineEditEnable)
        m_list = m_sql->InquirePurchaseOrderForm(ui->lineEditDateStart->text(),
                                             ui->lineEditDateEnd->text());
    else
        m_list = m_sql->InquirePurchaseOrderForm("all", "all");

    QStringList headers;
    headers << tr("进货日期") << tr("进货单号") << tr("供货商") << tr("操作人员") << tr("备注信息");

    ui->tableWidgetPurchaseOrderForm->setColumnCount(5);
    ui->tableWidgetPurchaseOrderForm->setHorizontalHeaderLabels(headers);

    setQtableWidgetHeaderStyle();

        //显示进货订单信息
    ui->tableWidgetPurchaseOrderForm->setRowCount(m_list.count());

    for(int i = 0; i < m_list.count(); ++i)
    {
        QTableWidgetItem *purchaseOrderFormNumber      = new QTableWidgetItem(
                    m_list.at(i).get_purchaseOrderFormNumber());
        QTableWidgetItem *supplier         = new QTableWidgetItem(
                    m_list.at(i).get_supplier());
        QTableWidgetItem *purchaseDate              = new QTableWidgetItem(
                    m_list.at(i).get_purchaseDate());
        QTableWidgetItem *staff        = new QTableWidgetItem(
                    m_list.at(i).get_staff());
        QTableWidgetItem *remark                 = new QTableWidgetItem(
                    m_list.at(i).get_remark());

        //设置商品编号,单价 数量 合计 为居中对齐
        purchaseOrderFormNumber->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        supplier->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        purchaseDate->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        staff->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
        remark->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );

        ui->tableWidgetPurchaseOrderForm->setItem(i, 0, purchaseDate);
        ui->tableWidgetPurchaseOrderForm->setItem(i, 1, purchaseOrderFormNumber);
        ui->tableWidgetPurchaseOrderForm->setItem(i, 2, supplier);
        ui->tableWidgetPurchaseOrderForm->setItem(i, 3, staff);
        ui->tableWidgetPurchaseOrderForm->setItem(i, 4, remark);
    }

    ui->tableWidgetPurchaseOrderForm->setCurrentItem(
                ui->tableWidgetPurchaseOrderForm->item(m_list.count() - 1, 0));

    setTableWidgetColor();
}

void PurchaseReceipt::initLineEditTime()
{
    ui->lineEditDateStart->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd"));
    ui->lineEditDateEnd->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd"));
    ui->tableWidgetPurchaseOrderForm->clear();
    ui->tableWidgetPurchaseOrderForm->setRowCount(0);
    QStringList headers;
    headers << tr("进货日期") << tr("进货单号") << tr("供货商") << tr("操作人员") << tr("备注信息");
    ui->tableWidgetPurchaseOrderForm->setColumnCount(5);
    ui->tableWidgetPurchaseOrderForm->setHorizontalHeaderLabels(headers);
    setQtableWidgetHeaderStyle();
}

void PurchaseReceipt::setTableWidgetColor()
{
    for (int i = 0; i < ui->tableWidgetPurchaseOrderForm->rowCount(); ++i)
    {
        if (i % 2 == 0)
        {
            for (int j = 0; j < ui->tableWidgetPurchaseOrderForm->columnCount(); ++j)
            {
                QTableWidgetItem *item = ui->tableWidgetPurchaseOrderForm->item(i,j);
                if (item)
                {
                    const QColor color = QColor(252, 222, 156);
                    item->setBackgroundColor(color);
                }
            }
        }
    }
}

void PurchaseReceipt::on_pushButtonInquire_clicked()
{
    int startYear = ui->lineEditDateStart->text().mid(0, 4).toInt();
    int startMonth = ui->lineEditDateStart->text().mid(5, 2).toInt();
    int startDay = ui->lineEditDateStart->text().mid(8, 2).toInt();

    int endYear = ui->lineEditDateEnd->text().mid(0, 4).toInt();
    int endMonth = ui->lineEditDateEnd->text().mid(5, 2).toInt();
    int endDay = ui->lineEditDateEnd->text().mid(8, 2).toInt();

    if ( (startYear > endYear) || (startYear == endYear && startMonth > endMonth)
         || (startYear == endYear && startMonth == endMonth && startDay > endDay) )
    {
        QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."),
                                 tr("\n开始日期大于结束日期，请重新设置") );
    }
    else
    {
        displayTableWidgetPurchaseOrderForm();
    }
}

void PurchaseReceipt::on_comboBox_activated(int index)
{
    QDateTime currentTime = QDateTime::currentDateTime();//获取系统现在的时间
//    QDateTime startTime, endTime;
    QString startTimeString, endTimeString;
    switch (index)
    {
        case 0 :  //今天
            startTimeString = currentTime.toString("yyyy-MM-dd");
            endTimeString = startTimeString;
            break;
        case 1 :  //昨天
            startTimeString = currentTime.addDays(-1).toString("yyyy-MM-dd");
            endTimeString = startTimeString;
            break;
        case 2 :  //本月
            startTimeString = currentTime.toString("yyyy-MM-dd").replace(8, 2, "01");
            endTimeString = currentTime.toString("yyyy-MM-dd");
            break;
        case 3 :  //上月
            startTimeString = currentTime.addMonths(-1).toString("yyyy-MM-dd").replace(8, 2, "01");
            endTimeString = currentTime.addDays(currentTime.toString("d").toInt() * -1)
                    .toString("yyyy-MM-dd");
            break;
        case 4 :  //今年
            startTimeString = currentTime.toString("yyyy-MM-dd").replace(5, 5, "01-01");
            endTimeString = currentTime.toString("yyyy-MM-dd");
            break;
        case 5 :  //去年
            startTimeString = currentTime.addYears(-1).toString("yyyy-MM-dd").replace(5, 5, "01-01");
            endTimeString = currentTime.addYears(-1).toString("yyyy-MM-dd").replace(5, 5, "12-31");
            break;
        case 6 :  //全部
            ui->lineEditDateStart->setText(tr("全部"));
            ui->lineEditDateEnd->setText(tr("全部"));
            ui->lineEditDateStart->setDisabled(true);
            ui->lineEditDateEnd->setDisabled(true);
            LineEditEnable = false;
            return;
        default :
            break;
    }
    ui->lineEditDateStart->setDisabled(false);
    ui->lineEditDateEnd->setDisabled(false);
    LineEditEnable = true;
    ui->lineEditDateStart->setText(startTimeString);
    ui->lineEditDateEnd->setText(endTimeString);
}

void PurchaseReceipt::on_tableWidgetPurchaseOrderForm_cellDoubleClicked(int row, int)
{
//    qDebug() << ui->tableWidgetPurchaseOrderForm->item(row, 1)->text();
    emit signalDiaplayPurchaseDetail(ui->tableWidgetPurchaseOrderForm->item(row, 1)->text(),
                                     m_list.at(row));
//    qDebug() << m_list.at(row).get_supplier();
    this->hide();
}
